package com.summer.data.rs.map;

import com.summer.data.dialect.Dialect;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 345480567@qq.com
 */

public class ResultSetToMapFun extends AbstractResultSetMapFun<Map<String, Object>> {

    @Override
    public Map<String, Object> map(ResultSet resultSet, Dialect dialect) throws SQLException {
        // todo: ydfqy
        Map<String, Object> rowMap = new HashMap<>();
        try {
            ResultSetMetaData metaData = resultSet.getMetaData();

            for(Integer i=1; i<=metaData.getColumnCount(); i++) {
                String label = dialect.isResultUseColumnName() ? metaData.getColumnName(i) : metaData.getColumnLabel(i);
                Object val = resultSet.getObject(i);
                rowMap.put(label, val);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return rowMap;
    }

}

